package service;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Date;

import model.QuestionModel;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import translate.BingTranslator;
import translate.GoogleTranslator;
import translate.YahooTranslator;
import entity.Question;

public class MyTranslator {

	private static int turn = 0;

	// translate Question with content from mongodb.
	public static Question translateFromDB(Question translateQuestion,
			String fromLang, String toLang) throws UnknownHostException,
			IOException {
		Document doc = null;

		// translate question
		String content = "";
		doc = Jsoup.parseBodyFragment("<div id=\"fullQuestionBody\">"
				+ translateQuestion.getContent() + "</div>");
		Elements myElements = doc.getElementById("fullQuestionBody").children();
		for (Element element : myElements) {
			String tagName = element.tagName();
			String elementText = element.text();
			if (tagName.equalsIgnoreCase("p")) {
				content += "<p>"
						+ manageTranslate(elementText, fromLang, toLang)
						+ "</p>\n";
			} else {
				content += element;
				content += "\n";
			}
		}
		translateQuestion.setContent(content);

		System.out.println("------------------------");
		// translate title
		translateQuestion.setTitle(manageTranslate(
				translateQuestion.getTitle(), fromLang, toLang));

		System.out.println("------------------------");
		// translate answer
		doc = null;
		String contentAnswer = "";
		doc = Jsoup.parseBodyFragment("<div id=\"fullAnswer\">"
				+ translateQuestion.getContentAnswer() + "</div>");
		Elements myAnswerElements = doc.getElementById("fullAnswer").children();
		for (Element element : myAnswerElements) {
			String tagName = element.tagName();
			String elementText = element.text();
			if (tagName.equalsIgnoreCase("p")) {
				contentAnswer += "<p>"
						+ manageTranslate(elementText, fromLang, toLang)
						+ "</p>\n";
			} else {
				contentAnswer += element;
				contentAnswer += "\n";
			}
		}
		translateQuestion.setContentAnswer(contentAnswer);
		translateQuestion.setLang(toLang);
		Date date = new Date();
		translateQuestion.setDoc(date);
		return translateQuestion;
	}

	// share translate work to google.translate, bing.translate and other. So
	// will not be banned.
	public static String manageTranslate(String text, String fromLang,
			String toLang) throws UnknownHostException, IOException {
		if (turn == 0 || turn == 2 || turn == 4 || turn == 6) {
			turn = turn + 1;
			String result = GoogleTranslator.translate(text, fromLang, toLang);
			System.out.println("from google : " + result); 
			return result;
		}
		if (turn == 8) {
			turn = 0;
			String result = BingTranslator.translate(text, fromLang, toLang);
			System.out.println("from bing : " + result); 
			return result;
		}
		if (turn == 1 || turn == 3 || turn == 5 || turn == 7) {			
			if (toLang == "zh" || toLang == "nl" || toLang == "fr"
					|| toLang == "de" || toLang == "el" || toLang == "it"
					|| toLang == "ja" || toLang == "ko" || toLang == "pt"
					|| toLang == "ru" || toLang == "es") {
				turn = turn + 1;
				String result = YahooTranslator.translate(text, fromLang,
						toLang);
				System.out.println("from yahoo : " + result);
				return result;
			} else {
				turn = 8;
				return GoogleTranslator.translate(text, fromLang, toLang);
			}

		}
		return text;
	}

	// translate Question from url.
	@SuppressWarnings("unused")
	public static void translateFromNativeSource() throws UnknownHostException,
			IOException {
		Document doc = null;
		QuestionModel questionModel = new QuestionModel();
		Question q = questionModel.findByAlias(
				"jquery-why-is-my-div-smaller-than-its-contents", "en");
		doc = util.SocketContentHandle.getSiteContentBySocket("efreedom.com",
				"",
				"http://efreedom.com/Question/1-714809/DIV-Smaller-Contents",
				"");
		Elements myE = doc.getElementById("fullQuestionBody").children();
		String content = "";
		for (Element element : myE) {
			String tagName = element.tagName();
			String elementText = element.text();
			if (tagName.equalsIgnoreCase("p")) {
				content += "<p>"
						+ GoogleTranslator.translate(elementText, "en", "vi")
						+ "</p>";
			}
			if (tagName.equalsIgnoreCase("pre")) {
				content += element;
			}
		}
		System.out.println(content);
	}
}
